namespace TwoColorBalls
{
	public partial class Form1 : Form
	{
		private bool start = false;
		List<Label> labels = new();
		private Task? task;

		public Form1()
		{
			InitializeComponent();
			button1.Click += Start;
			button2.Click += Stop;
			labels.AddRange([
				rb1, rb2, rb3, rb4, rb5, rb6, bb1
			]);
		}

		private void Start(object? sender, EventArgs e)
		{
			button2.Enabled = start = true;
			button1.Enabled = !start;
			task = new Task(Starting);
			task.Start();
		}

		private void Stop(object? sender, EventArgs e)
		{
			button2.Enabled = start = false;
			button1.Enabled = !start;
			task = null;
		}

		private void Starting()
		{
			while (start) 
			{
				foreach (Label label in labels)
				{
					int i = 0;
					if (label.BackColor == Color.Blue)
					{
						i = new Random().Next(1, 16);
					}
					else
					{
						i = new Random().Next(1, 33);
					}
					string result = i < 10 ? $"0{i}" : i.ToString();
					label.Invoke(() =>
					{
						label.Text = result;
					});
				}
			}
		}
	}
}
